﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;
using ISTKPlugin;
using BDSim.用户界面.Models;
using System.Collections.ObjectModel;
using System.Reflection;

namespace BDSim.用户界面.SubWindows
{
    /// <summary>
    /// PluginManager.xaml 的交互逻辑
    /// </summary>
    public partial class PluginManager : Window
    {
        public PluginManager()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog opf = new OpenFileDialog();
            if (opf.ShowDialog() == true)
            {
                InterfaceSTKPlugin stkPlugin = controlName(opf.FileName);
                if (stkPlugin != null)
                {
                    Plugin plugin = new Plugin() {Name = opf.SafeFileName,Version = "1.0", FilePath = opf.FileName };
                    (DataContext as MainWindowViewModel).PluginList.Add(plugin);
                    XmlFileOperater<ObservableCollection<Plugin>>.SaveToFile("pluginList.xml", (DataContext as MainWindowViewModel).PluginList);
                }
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Plugin selectedPlugin = (listView1.SelectedItem as Plugin);
            if (MessageBox.Show("确定删除此插件" + selectedPlugin.Name + "吗？", "提醒", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                (DataContext as MainWindowViewModel).PluginList.Remove(selectedPlugin);
                XmlFileOperater<ObservableCollection<Plugin>>.SaveToFile("pluginList.xml", (DataContext as MainWindowViewModel).PluginList);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        InterfaceSTKPlugin controlName(string filename)
        {
            try
            {
                Type type = null;
                // 获取程序集
                Assembly asm = Assembly.LoadFrom(filename);
                foreach (Type t in asm.GetTypes())
                {
                    //搜索Itools接口，查看是否有与接口匹配的项
                    if (t.GetInterface("InterfaceSTKPlugin") != null)
                    {
                        type = t;
                        break;
                    }
                }
                //把继承Itools接口的类实例化并转换为Itools类型
                if (type != null)
                {
                    ConstructorInfo info = null;
                    InterfaceSTKPlugin toolCOM = null;
                    try
                    {
                        info = type.GetConstructor(new Type[] { });
                        object result = info.Invoke(new Object[] { });
                        toolCOM = (InterfaceSTKPlugin)result;
                    }
                    catch (Exception exp)
                    {

                    }
                    return toolCOM;
                }
                return null;
            }
            catch
            { return null; }
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }
    }
}
